<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>05.Python循环语句 | 王扣笔记</title><meta name="author" content="王扣"><meta name="copyright" content="王扣"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python入门课程  循环语句   章节目录 [!WARNING|label:本章需要学习的内容]   while循环的基础语法 while循环的基础案例 while循环的嵌套应用 while循环的嵌套案例 for循环的基础语法 for循环的嵌套应用 循环中断 : break和continue 综合案例   while循环的基础语法   练习题 代码 求1-100的和  12345678# 练习">
<meta property="og:type" content="article">
<meta property="og:title" content="05.Python循环语句">
<meta property="og:url" content="http://grace.666443.xyz/posts/faab6e1.html">
<meta property="og:site_name" content="王扣笔记">
<meta property="og:description" content="Python入门课程  循环语句   章节目录 [!WARNING|label:本章需要学习的内容]   while循环的基础语法 while循环的基础案例 while循环的嵌套应用 while循环的嵌套案例 for循环的基础语法 for循环的嵌套应用 循环中断 : break和continue 综合案例   while循环的基础语法   练习题 代码 求1-100的和  12345678# 练习">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/0df4f1a3cf2f76812e9cbf25c3190113.png">
<meta property="article:published_time" content="2022-12-08T11:42:24.000Z">
<meta property="article:modified_time" content="2022-12-08T12:09:02.840Z">
<meta property="article:author" content="王扣">
<meta property="article:tag" content="笔记 开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/0df4f1a3cf2f76812e9cbf25c3190113.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://grace.666443.xyz/posts/faab6e1"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '05.Python循环语句',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-12-08 20:09:02'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/0df4f1a3cf2f76812e9cbf25c3190113.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">王扣笔记</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">05.Python循环语句</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-12-08T11:42:24.000Z" title="发表于 2022-12-08 19:42:24">2022-12-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-12-08T12:09:02.840Z" title="更新于 2022-12-08 20:09:02">2022-12-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python%E5%9F%BA%E7%A1%80/">python基础</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="05.Python循环语句"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python入门课程-循环语句"><a href="#Python入门课程-循环语句" class="headerlink" title="Python入门课程  循环语句"></a>Python入门课程  循环语句</h1><hr>
<p><img src="https://img-blog.csdnimg.cn/641be0e9916e424da3de00c6999dd0d2.png" alt="1.jpg"></p>
<hr>
<h2 id="章节目录"><a href="#章节目录" class="headerlink" title="章节目录"></a><strong>章节目录</strong></h2><blockquote>
<p>[!WARNING|label:本章需要学习的内容]</p>
</blockquote>
<ul>
<li>while循环的基础语法</li>
<li>while循环的基础案例</li>
<li>while循环的嵌套应用</li>
<li>while循环的嵌套案例</li>
<li>for循环的基础语法</li>
<li>for循环的嵌套应用</li>
<li>循环中断 : break和continue</li>
<li>综合案例</li>
</ul>
<hr>
<h2 id="while循环的基础语法"><a href="#while循环的基础语法" class="headerlink" title="while循环的基础语法"></a><strong>while循环的基础语法</strong></h2><hr>
<p><img src="https://img-blog.csdnimg.cn/d513e5dcf82945ad917f7158465934c3.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/c849d4e0aa77416ca819e1d22abfbc9b.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/e0c01aa6a9484101a21be36b2d93fb83.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/dcac865755994a05a6e497f822831221.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/bdc97e0f004e4fee888721aa1b641cbf.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/d686f2d03f09412d942df11a343c5527.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/5d1728bedbf449129b9e44343c479113.png" alt="8.jpg"></p>
<blockquote>
<p>练习题 代码 求1-100的和</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 练习题 1-100和</span></span><br><span class="line">num = <span class="number">1</span></span><br><span class="line">count = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> num &lt;= <span class="number">100</span>:</span><br><span class="line">    count += num</span><br><span class="line">    num += <span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;1-100的和为：<span class="subst">&#123;count&#125;</span>&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="while循环的基础案例"><a href="#while循环的基础案例" class="headerlink" title="while循环的基础案例"></a><strong>while循环的基础案例</strong></h2><p><img src="https://img-blog.csdnimg.cn/7e556c1c760f46ef957df60837020d08.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/8c557b5dd6d64bdd828d8f2835fed079.png" alt="2.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 设置一个随机整数1-100范围</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line">num = random.randint(<span class="number">1</span>, <span class="number">100</span>)</span><br><span class="line">count = <span class="number">0</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;猜测的数字结果是：<span class="subst">&#123;num&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">flag = <span class="literal">True</span></span><br><span class="line"><span class="keyword">while</span> flag:</span><br><span class="line">    count += <span class="number">1</span></span><br><span class="line">    guess_num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入猜的数字:&quot;</span>))</span><br><span class="line">    <span class="keyword">if</span> guess_num == num:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;猜对了&quot;</span>)</span><br><span class="line">        flag = <span class="literal">False</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">if</span> guess_num &gt;= num:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;猜大了&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;猜小了&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;猜完了，猜的总次数：<span class="subst">&#123;count&#125;</span>&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="while循环的嵌套应用"><a href="#while循环的嵌套应用" class="headerlink" title="while循环的嵌套应用"></a><strong>while循环的嵌套应用</strong></h2><p><img src="https://img-blog.csdnimg.cn/a141d3f92bbb49b09c33dfbadbb3b879.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/33c4b376962e49c08749d963ce9205a7.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/14d6e216a3e44d5e85966c631d249c8a.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/93154cd903064d39b971a578a178ca7a.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/c925f08cd62d4a16b3e16f9177fcc439.png" alt="5.jpg"></p>
<h2 id="while循环的嵌套案例"><a href="#while循环的嵌套案例" class="headerlink" title="while循环的嵌套案例"></a><strong>while循环的嵌套案例</strong></h2><p><img src="https://img-blog.csdnimg.cn/229b7a95f913403d9bb033c8a7bdd61b.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/2f733fda1c6d4ed69148fd85b385cace.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/9c6cbfd7719e40e5a936161f00d664cc.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/867bf9ea63f14f69a7edb27b861ed6ba.png" alt="4.jpg"></p>
<blockquote>
<p>练习题代码： 输出九九乘法表</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义外层循环变量 i</span></span><br><span class="line">i = <span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> i &lt;= <span class="number">9</span>:</span><br><span class="line">    <span class="comment"># 定义内层循环变量 j</span></span><br><span class="line">    j = <span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> j &lt;= i:</span><br><span class="line">        <span class="comment"># 内层不换行</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;<span class="subst">&#123;i&#125;</span> * <span class="subst">&#123;j&#125;</span> = <span class="subst">&#123;i * j&#125;</span> \t&quot;</span>,end=<span class="string">&quot;&quot;</span>)</span><br><span class="line">        j += <span class="number">1</span></span><br><span class="line">    <span class="comment"># 外层换行 print输出为空，即换行</span></span><br><span class="line">    <span class="built_in">print</span>()</span><br><span class="line">    i += <span class="number">1</span></span><br></pre></td></tr></table></figure>

<h2 id="for循环的基础语法"><a href="#for循环的基础语法" class="headerlink" title="for循环的基础语法"></a><strong>for循环的基础语法</strong></h2><ul>
<li> ①基础语法</li>
<li> ②range语句</li>
<li> ③变量作用域</li>
</ul>
<blockquote>
<p>[!tip|style:callout|label:①基础语法] </p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/71127a9133df4fb7b8bd53e2bdc36e27.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/32f0dd552aba430c94e43733e49df002.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/631470c2cd95493795fb7bdd897bdb3e.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/c73947cd404c4c50949fbcf553461456.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/103748cdf80a41cca6b4f884266e64ab.png" alt="5.jpg"></p>
<blockquote>
<p>====================================== 练 习 题 ======================================<br><img src="https://img-blog.csdnimg.cn/f3274bd2e05e40c3aba766a25b155334.png" alt="6.jpg"></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 计数变量</span></span><br><span class="line">num = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字符串</span></span><br><span class="line">name = <span class="string">&quot;itheima is abrand of itcast&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># for 循环</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> name:</span><br><span class="line">    <span class="keyword">if</span> x == <span class="string">&quot;a&quot;</span>:</span><br><span class="line">        num += <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;被统计 的字符串共有<span class="subst">&#123;num&#125;</span>个字母a&quot;</span>)</span><br></pre></td></tr></table></figure>

<hr>
<blockquote>
<p>[!tip|style:callout|label:②range语句] </p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/3e11e5164c8f4541965dd11d20bfde25.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/4bfc4e13a500456f88da5541d65ac64d.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/a3dc959261db45efa2e36bf20f6a4010.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/9da174e03caa4e49ad0af5e48313f314.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/ddfc2c6d9001441c971e1af323523af1.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/7d7f0bb4c5b84bb0ae17b7b0a113cafc.png" alt="6.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个变量</span></span><br><span class="line">num = <span class="number">100</span></span><br><span class="line"><span class="comment"># 统计偶数出现次数变量</span></span><br><span class="line">count = <span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, num):</span><br><span class="line">    <span class="built_in">print</span>(element)</span><br><span class="line">    <span class="keyword">if</span> element % <span class="number">2</span> == <span class="number">0</span>:</span><br><span class="line">        count += <span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;一共出现偶数次数：<span class="subst">&#123;count&#125;</span>次&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<blockquote>
<p>[!tip|style:callout|label:③变量作用域] </p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/7e3866933e6c44ea916bc4d061115da8.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/96211c21833b44a8a9bb417effc2fec3.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/d44b814cf593455d95b6140672d28eba.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/5155b628382340afb9021cb7deb16c42.png" alt="4.jpg"></p>
<hr>
<h2 id="for循环的嵌套应用"><a href="#for循环的嵌套应用" class="headerlink" title="for循环的嵌套应用"></a><strong>for循环的嵌套应用</strong></h2><p><img src="https://img-blog.csdnimg.cn/0030972e79c5415b994513009b34745c.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/a48483315d6d43d7873359c5cc918f01.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/806184c71a7648dcb9f8cd10c4e26805.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/dab49ef5d10a4c11ba080056d92e5de0.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/11b934aff0754cb3a56c52eb61aa8c18.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/fdffe4af02c84d03a41b07652fd3aabc.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/182ba231346549d0a973b09662dacd50.png" alt="7.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">10</span>):</span><br><span class="line">    <span class="comment"># 定义内层循环变量 j</span></span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, i + <span class="number">1</span>):</span><br><span class="line">        <span class="comment"># 内层不换行</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;<span class="subst">&#123;j&#125;</span> * <span class="subst">&#123;i&#125;</span> = <span class="subst">&#123;i * j&#125;</span> \t&quot;</span>, end=<span class="string">&quot;&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 外层换行 print输出为空，即换行</span></span><br><span class="line">    <span class="built_in">print</span>()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="循环中断-break和continue"><a href="#循环中断-break和continue" class="headerlink" title="循环中断:break和continue"></a><strong>循环中断:break和continue</strong></h2><p><img src="https://img-blog.csdnimg.cn/0ea9d953be814fc9aabaaf2d5a2a1123.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/77499565c16b4d0d9e5b8ad6c289e2fa.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/8a2695c5db8a4b6781ed43fa2886e3c5.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/31194333e5154d9e8f8da0ec86f7990b.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/6496fd480bf648a8a95009e919a8b227.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/e9e32320942947bda9e94a841c6dd0c8.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/7093a7ea30b748858c823fa598952916.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/8670eb188c5e411ab0cc3e3ac84180fd.png" alt="8.jpg"><br><img src="https://img-blog.csdnimg.cn/923331bb179e4780a3a1dee81625e762.png" alt="9.jpg"></p>
<h2 id="综合案例"><a href="#综合案例" class="headerlink" title="综合案例"></a><strong>综合案例</strong></h2><p><img src="https://img-blog.csdnimg.cn/8a26da8a799b4d59837ef07d300a7d13.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/3176c276af7c4c6e887ba948d759f4cb.png" alt="2.jpg"></p>
<blockquote>
<p><strong>练习题代码</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment"># 定义账户余额变量</span></span><br><span class="line">Money = <span class="number">10000</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># for 循环对员工发工资</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">21</span>):</span><br><span class="line">    <span class="keyword">import</span> random</span><br><span class="line">    score = random.randint(<span class="number">1</span>, <span class="number">10</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 判断绩效分够不够</span></span><br><span class="line">    <span class="keyword">if</span> score &lt; <span class="number">5</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;员工编号：<span class="subst">&#123;i&#125;</span> 的绩效分<span class="subst">&#123;score&#125;</span>,不满足，不发工资，下一位&quot;</span>)</span><br><span class="line">        <span class="comment"># continue跳过发放</span></span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 要判断余额足不足</span></span><br><span class="line">    <span class="keyword">if</span> Money &gt;= <span class="number">1000</span>:</span><br><span class="line">        Money -= <span class="number">1000</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;员工<span class="subst">&#123;i&#125;</span>,满足发放工资条件，发放1000元，公司账户余额:<span class="subst">&#123;Money&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;余额不足，当前余额「<span class="subst">&#123;Money&#125;</span>」元,不足以发工资，不发了,结束发工资，下个月再来&quot;</span>)</span><br><span class="line">        <span class="keyword">break</span> </span><br><span class="line"></span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://grace.666443.xyz">王扣</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://grace.666443.xyz/posts/faab6e1.html">http://grace.666443.xyz/posts/faab6e1.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://grace.666443.xyz" target="_blank">王扣笔记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/0df4f1a3cf2f76812e9cbf25c3190113.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/b2d84ecf.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/561b31d5498e9c25ac5416d4.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">06.Python函数</div></div></a></div><div class="next-post pull-right"><a href="/posts/9b6113bf.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/f477c264e6f1e4c9138450a3a0359907.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">04.Python判断语句</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">王扣</div><div class="author-info__description">常用开发中的笔记记录</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%85%A5%E9%97%A8%E8%AF%BE%E7%A8%8B-%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.</span> <span class="toc-text">Python入门课程  循环语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AB%A0%E8%8A%82%E7%9B%AE%E5%BD%95"><span class="toc-number">1.1.</span> <span class="toc-text">章节目录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">while循环的基础语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%9F%BA%E7%A1%80%E6%A1%88%E4%BE%8B"><span class="toc-number">1.3.</span> <span class="toc-text">while循环的基础案例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%B5%8C%E5%A5%97%E5%BA%94%E7%94%A8"><span class="toc-number">1.4.</span> <span class="toc-text">while循环的嵌套应用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%B5%8C%E5%A5%97%E6%A1%88%E4%BE%8B"><span class="toc-number">1.5.</span> <span class="toc-text">while循环的嵌套案例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#for%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">1.6.</span> <span class="toc-text">for循环的基础语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#for%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%B5%8C%E5%A5%97%E5%BA%94%E7%94%A8"><span class="toc-number">1.7.</span> <span class="toc-text">for循环的嵌套应用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E4%B8%AD%E6%96%AD-break%E5%92%8Ccontinue"><span class="toc-number">1.8.</span> <span class="toc-text">循环中断:break和continue</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B"><span class="toc-number">1.9.</span> <span class="toc-text">综合案例</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/ae1a0acd.html" title="18.Python爬虫-requests模块"><img src="https://p.ananas.chaoxing.com/star3/origin/b230c5f9060b1f422a86e332d8e37926.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="18.Python爬虫-requests模块"/></a><div class="content"><a class="title" href="/posts/ae1a0acd.html" title="18.Python爬虫-requests模块">18.Python爬虫-requests模块</a><time datetime="2022-12-08T12:53:30.000Z" title="发表于 2022-12-08 20:53:30">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/ed7a56b2.html" title="17.Python爬虫-数据解析与提取"><img src="https://p.ananas.chaoxing.com/star3/origin/8f2179d04bfe0e48cc706ae112c97952.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="17.Python爬虫-数据解析与提取"/></a><div class="content"><a class="title" href="/posts/ed7a56b2.html" title="17.Python爬虫-数据解析与提取">17.Python爬虫-数据解析与提取</a><time datetime="2022-12-08T12:52:55.000Z" title="发表于 2022-12-08 20:52:55">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5038ed.html" title="16.Python爬虫-基础"><img src="https://p.ananas.chaoxing.com/star3/origin/2054c03c91d338203c730610ead1b303.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="16.Python爬虫-基础"/></a><div class="content"><a class="title" href="/posts/5038ed.html" title="16.Python爬虫-基础">16.Python爬虫-基础</a><time datetime="2022-12-08T12:52:11.000Z" title="发表于 2022-12-08 20:52:11">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/2b5a0751.html" title="15.Python之Mysql数据库相关"><img src="https://p.ananas.chaoxing.com/star3/origin/2a1821704670b5cf8c2289255e183d62.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="15.Python之Mysql数据库相关"/></a><div class="content"><a class="title" href="/posts/2b5a0751.html" title="15.Python之Mysql数据库相关">15.Python之Mysql数据库相关</a><time datetime="2022-12-08T12:17:02.000Z" title="发表于 2022-12-08 20:17:02">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/6ebaa02b.html" title="14.Python面向对象"><img src="https://p.ananas.chaoxing.com/star3/origin/d24d9bfcd4ce3f910d6b5cdc78676d75.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="14.Python面向对象"/></a><div class="content"><a class="title" href="/posts/6ebaa02b.html" title="14.Python面向对象">14.Python面向对象</a><time datetime="2022-12-08T12:10:47.000Z" title="发表于 2022-12-08 20:10:47">2022-12-08</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 王扣</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><div class="fb-customerchat" id="fb-customer-chat" attribution="biz_inbox"></div><script>document.getElementById('fb-root') ? '' : document.body.insertAdjacentHTML('afterend', '<div id="fb-root"></div>')

window.fbAsyncInit = function() {
  FB.init({
    xfbml: true,
    version: 'v15.0'
  });
};

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = 'https://connect.facebook.net/zh_CN/sdk/xfbml.customerchat.js';
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

if (false) {
  var chatBtnFn = () => {
    var chatBtn = document.getElementById("chat_btn")
    chatBtn.addEventListener("click", function(){
      FB.CustomerChat.show();
    });
  }
  chatBtnFn()
} else {
  if (false) {
    function chatBtnHide () {
      FB.CustomerChat.hide()
    }
    function chatBtnShow () {
      FB.CustomerChat.show(false)
    }
  }
}</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>